package vn.buffalocoders.rikos.ui.common;

import java.util.ArrayList;

import vn.buffalocoders.rikos.R;
import vn.buffalocoders.rikos.ui.network.ConnectRikoServer;
import vn.buffalocoders.rikos.ui.network.DownloadImageTask;

import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomOptionMenu {
	/**
	 * Some global variables.
	 */
	private ArrayList<CustomOptionMenuItem> mMenuItems;
	private OnMenuItemSelectedListener mListener = null;
	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private LinearLayout mLinearLayout = null;
	private boolean mIsShowing = false;
	private boolean mHideOnSelect = true;
	private int mItemHeight;
	
	/**
	 * The interface for returning the item clicked.
	 */
	public interface OnMenuItemSelectedListener {
		public void MenuItemSelectedEvent(CustomOptionMenuItem selection);
	}
	
	/**
	 * Use this method to determine if the menu is currently displayed to the user.
	 * @return boolean isShowing
	 */	
	public boolean isShowing() { return mIsShowing; }
	
	/**
	 * This setting controls whether or not the menu closes after an item is selected.
	 * @param boolean doHideOnSelect
	 * @return void
	 */	
	public void setHideOnSelect(boolean doHideOnSelect) { mHideOnSelect = doHideOnSelect; } 
	

	/**
	 * Use this method to assign your menu items. You can only call this when the menu is hidden.
	 * @param ArrayList<CustomOptionMenuItem> items
	 * @return void
	 * @throws Exception "Menu list may not be modified while menu is displayed."
	 */	
	public synchronized void setMenuItems(ArrayList<CustomOptionMenuItem> items) throws Exception {
		if (mIsShowing) {
			throw new Exception("Menu list may not be modified while menu is displayed.");
		}
		mMenuItems = items;
	}
	
	/**
	 * This is our constructor.  Note we require a layout inflater.  There is probably a way to
	 * grab one of these from the local context but I failed to find it.
	 * @param Context context
	 * @param OnMenuItemSelectedListener listener
	 * @param LayoutInflater lo
	 * @return void
	 */	
	public CustomOptionMenu(Context context, LinearLayout ll, OnMenuItemSelectedListener listener, LayoutInflater lo, int iItemHeight) {
		mListener = listener;
		mMenuItems = new ArrayList<CustomOptionMenuItem>();
		mContext = context;
		mLayoutInflater = lo;
		mLinearLayout = ll;
		mItemHeight = iItemHeight;
	}
	
	public synchronized void initOptionMenu() {
		int itemCount = mMenuItems.size();
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int itemWidth = 115;
		if (display.getWidth() > itemCount * itemWidth)
		{
			int diff = display.getWidth() - itemCount * itemWidth;
			itemWidth = itemWidth + diff / (itemCount - 1);
		}
		
		if (mLinearLayout.getChildCount() > 0)
		{
			mLinearLayout.removeAllViews();
		}
		for (int i = 0; i < itemCount; i++){
			final CustomOptionMenuItem cmi = mMenuItems.get(i);
			//View view = mLayoutInflater.inflate(R.layout.custom_menu_item, mLinearLayout,false);
			LinearLayout itemLayout = (LinearLayout)mLayoutInflater.inflate(R.layout.custom_menu_item, mLinearLayout,false);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemWidth, mItemHeight);
			itemLayout.setLayoutParams(params);
			TextView tv = (TextView)itemLayout.findViewById(R.id.custom_menu_item_caption);
			tv.setText(cmi.getCaption());
			ImageView iv = (ImageView)itemLayout.findViewById(R.id.custom_menu_item_icon);
			if (cmi.getImageResourceId() != -1) {
				iv.setImageResource(cmi.getImageResourceId());
			}
			else if (!TextUtils.isEmpty(cmi.getImageURL())) {
				new DownloadImageTask((ImageView) iv)
                .execute(String.format("%s%s", ConnectRikoServer.URL_IMAGE_HOME, cmi.getImageURL()));
			}
			itemLayout.setOnClickListener( new OnClickListener() {
			   @Override
			   public void onClick(View v) {
					mListener.MenuItemSelectedEvent(cmi);
					// if (mHideOnSelect) hide();
			   }
			});
			mLinearLayout.addView(itemLayout);
		}
	}
}
